package com.excuse.excusebuilder.model;

import com.excuse.excusebuilder.bean.Post;
import com.excuse.excusebuilder.impl.callback.CommunityCallBack;
import com.excuse.excusebuilder.impl.model.CommunityModelInterface;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by U-nookia on 2017/4/18.
 */

public class CommunityModel implements CommunityModelInterface {

    private List<Post> posts;

    public CommunityModel() {
        posts = new ArrayList<>();
    }

    @Override
    public void getPostsFromBmob(final CommunityCallBack callBack) {
        BmobQuery<Post> queryPosts = new BmobQuery<>();
        queryPosts.addWhereNotEqualTo("createdAt",0);
        queryPosts.include("user");
        queryPosts.order("-likeNum");
        queryPosts.setLimit(1000);
        queryPosts.findObjects(new FindListener<Post>() {
            @Override
            public void done(List<Post> list, BmobException e) {
                if (e!=null){
                    callBack.hasError(e.toString());
                    return;
                }
                posts = list;
                callBack.getPosts(posts);
            }
        });
    }
}
